package day09;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class Demo01 {
    public static void main(String[] args) {
        JFrame f = new JFrame("捕鱼");
        f.setSize(800,510);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FishPanel fp = new FishPanel();
        f.add(fp);
        f.setVisible(true);
        fp.move();
    }
}//Demo01类结束
//FishPanel开始
class FishPanel extends JPanel{
    BufferedImage bg;
    BufferedImage fish01;
    int x = 400;
    int y = 100;
    public FishPanel(){
        try {
            bg = ImageIO.read(new File("images/bg.jpg"));
            fish01 = ImageIO.read(new File("images/fish09_00.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void move(){
        while(true){
            x-=3;
            if(x<-180){
                x = 800;
                y = (int)(Math.random()*480);
            }
            try {
                Thread.sleep(1000/24);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint();
        }
    }
    @Override
    public void paint(Graphics g) {
        g.drawImage(bg,0,0,null);
        g.drawImage(fish01,x,y,null);
    }

}
//FishPanel结束
//Fish类来时
class Fish{
    BufferedImage img;//鱼的图片
    int x;//鱼的x坐标
    int y;//鱼的y坐标
    int width;//鱼图片的宽度
    int height;//鱼图片的高度
    int step;//鱼游动的速度

    public Fish(String name){
        //images/fish01_00.png
        try {
            img = ImageIO.read(new File("images/"+name+"_00.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Random r = new Random();
        x = r.nextInt(800);
        y = r.nextInt(480);
        width = img.getWidth();
        height = img.getHeight();
        step = r.nextInt(8)+1;
    }
    public void move(){
        x -= step;
        if(x<-width){
            x = 800;
            y = (int)(Math.random()*480);
        }
    }

}
//Fish类结束




